package com.cxy.client;

import com.cxy.prototype.Address;
import com.cxy.prototype.Student;

import java.io.*;

public class PrototypeClientB {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("张三");
        student.setAge(18);
        student.setAddress(new Address("河南","郑州"));
        Student student1 = deepCopy(student);
        student1.setName("李四");
        student1.setAge(20);
        student1.setAddress(student1.getAddress().setProvince("湖北").setProvince("武汉"));
        System.out.println(student1);
        System.out.println(student);
    }

    public static <T> T deepCopy(T original){
        T copy = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(original);
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
            copy = (T)ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return copy;
    }
}
